﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Finance.DTO.PayRefund;
using Entity.Finance.Entity;
using Entity.Finance.View.PayRefund;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class PayRefundService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public PayRefundService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<PayRefundView>>> GetPayRefundList(PayRefundSearchDTO model)
        {
            //筛选
            var query = _context.PayRefundView
                .WhereIF(a => a.CustomerCode.Contains(model.Customer) || a.CustomerName.Contains(model.Customer), !string.IsNullOrEmpty(model.Customer))
                .WhereIF(a => a.Status == model.Status, model.Status != null)
                ;

            //返回列表
            var list = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<PayRefundView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        ///**
        // * 新增
        // */
        //public async Task<RequestResponseModel<string>> AddPayRefund(PayRefundAddDTO model)
        //{
        //    //新增数据
        //    var entity = _mapper.Map<PayRefundModel>(model);
        //    entity.CreateTime = DateTime.Now;
        //    entity.Id = Guid.NewGuid().ToString();

        //    //更新表
        //    _context.PayRefund.Add(entity);
        //    await _context.SaveChangesAsync();

        //    return RequestResponse.CreateResponse(200, "新增成功", entity.Id);
        //}

        ///**
        // * 更新
        // */
        //public async Task<RequestResponseModel<string>> PutPayRefund(PayRefundUpdateDTO model)
        //{
        //    //校验数据
        //    var query = await Valid.ValidId<PayRefundModel>(_context.PayRefund, model.Id);
        //    if (query == null)
        //    {
        //        return RequestResponse.CreateResponse(400, "Id无效", model.Id);
        //    }

        //    //编辑数据
        //    var entity = _mapper.Map(model, query);

        //    //更新表
        //    _context.PayRefund.Update(entity);
        //    await _context.SaveChangesAsync();

        //    return RequestResponse.CreateResponse(200, "更新成功", entity.Id);
        //}

        /**
         * 删除
         */
        public async Task<RequestResponseModel<string>> DeletePayRefund(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<PayRefundModel>(_context.PayRefund, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.PayRefund.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
